<?php
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;

import("Plugins.Form.Classes.DAO.FormResponseDAO");
import("Plugins.Form.Classes.Util.FormUtil");

$entry = $response->getAttribute("entry");
$theme = $response->getAttribute("form");
$category = $response->getAttribute("category");
if (is_null($category) && !is_null($entry)) $category = $entry->getCategory();

$jsonForm = $response->getAttribute("jsonForm");
$formResponse = $response->getAttribute("formResponse");
$formResponses = $response->getAttribute("formResponses");
$formResponseParameters = $response->getAttribute("formResponseParameters");
$responseId = $response->getAttribute("responseId");
$structure = $response->getAttribute("structure");
$part = $response->getAttribute("part");
$formContent = $response->getAttribute("formContent");

$responseIdDao = DAOFactory::getDAO('ResponseId');
?>
<article id="cont-princ" class="cols">
    <div style="float:right; text-align:right;">
        <?php echo HTML::link($languageManager->getText('PREVIEW'), 'action=Preview&entry_id='.$entry->getId(), array('class'=>'popup')); ?>
    </div>
    <h1>
        <?php Printer::display($languageManager->getText($theme->getTitle())) ?>
    </h1>
    <h4>
        <?php Printer::display($languageManager->getText($category->getProfile()->getText())); ?>
    </h4>
    <?php 
    if (!is_null($formResponses) && count($formResponses)>0) {
    ?>
    <div class="fillFromPrevious">
        <?php
        foreach ($formResponses as $previousResponse) {
            $previousResponseId = $responseIdDao->retrieveByPk($previousResponse->getFormResponseId());

            if (!is_null($previousResponseId) && $previousResponseId->getId() != $responseId->getId()) {
        ?>
        <form
            method="POST"
            action="<?php Printer::display(HTML::linkHref('action=SaveProfile'));?>"
            enctype="multipart/form-data">
            <?php
            if (!is_null($responseId)) {
            ?>
            <input
                type="hidden"
                name="id"
                value="<?php Printer::display($responseId->getId()) ?>" />
            <?php
            }
            if (!is_null($entry)) {
            ?>
            <input
                type="hidden"
                name="entry_id"
                value="<?php Printer::display($entry->getId()) ?>" />
            <?php
            }
            ?>
            <input
                type="hidden"
                name="form_id"
                value="<?php Printer::display($theme->getId()) ?>" />
            <input
                type="hidden"
                name="category_id"
                value="<?php if (!is_null($category)) Printer::display($category->getId()); ?>" />
            <input
                type="hidden"
                name="previous"
                value="<?php Printer::display($previousResponseId->getId()); ?>" />
            <input
                type="submit"
                value="<?php
                    Printer::display(
                        $languageManager->getText('COPY_FROM_ENTRY') . ' ' .
                        $previousResponseId->getEntry()->getName()
                    ); ?>" />
        </form>
        <?php
            }
        }
        ?>
    </div>
    <?php 
    }

    if (!is_null($jsonForm)) {
        
        $editable = true ;
        $formAction = 'action=SaveProfile' ;
        $submitText = 'SAVE_MODIFICATIONS' ;
        
        $categoryLink = '' ;
        if (!is_null($category)) $categoryLink = '&category_id=' . $category->getId();
        
        if (!$responseId->isActive() && !$part->isFree()) {
            $editable = false ;
            $formAction
                = "action=OrderTheme&entry_id=".$entry->getId().
        	    "&form_id=".$theme->getId().$categoryLink ;
            $submitText = 'UNLOCK_THEME' ;
        }
        
    ?>
    <form
        id="profileForm"
        class="editForm psiForm"
        method="POST"
        action="<?php Printer::display(HTML::linkHref($formAction));?>"
        enctype="multipart/form-data">
        <?php
        if (!is_null($responseId)) {
        ?>
        <input
            type="hidden"
            name="id"
            value="<?php Printer::display($responseId->getId()) ?>" />
        <?php
        }
        if (!is_null($entry)) { ?>
        <input
            type="hidden"
            name="entry_id"
            value="<?php Printer::display($entry->getId()) ?>" />
        <?php
        }
        ?>
        <input
            type="hidden"
            name="form_id"
            value="<?php Printer::display($theme->getId()) ?>" />
        <input
            type="hidden"
            name="category_id"
            value="<?php if (!is_null($category)) Printer::display($category->getId()); ?>" />

        <?php echo $formContent; ?>

        <hr />

        <fieldset class="floatRight">
            <input
                type="reset"
                name="reset"
                value="<?php Printer::display($languageManager->getText('CANCEL')); ?>">
            <input
                type="submit"
                name="inscription"
                value="<?php Printer::display($languageManager->getText($submitText)); ?>">
        </fieldset>
    </form>
    <?php
    }
    ?>
</article>
